package edu.chl.dat255.bluebanana.utils;

import android.content.Context;
import android.database.Cursor;
import edu.chl.dat255.bluebanana.data.impl.SQLiteStorage;

public class DatabaseUtil {
	private static SQLiteStorage db;
	
	/**
	 * 
	 * @param profileId The id of the profile to update
	 * @param dbKey The constant that tells the database what value to update
	 * @param value The new value 
	 * @param context The context from the class that requests the update
	 */
	public static void sendValueToDatabase(long profileId , String dbKey, Object value, Context context) {
		db = new SQLiteStorage(context);
        db.open();
        if (value.getClass().equals(String.class)) {
        	String stringValue = (String) value;
            db.updateValue(profileId, dbKey, stringValue);
        } else if (value.getClass().equals(Integer.class)) {
        	int intValue = (Integer) value;
            db.updateValue(profileId, dbKey, intValue);
        } else if (value.getClass().equals(Boolean.class)) {
        	boolean booleanValue = (Boolean) value;
            db.updateValue(profileId, dbKey, booleanValue);
        }
        db.close();
	}
	
	/**
	 * 
	 * @param profileId The id of the profile to update
	 * @param dbKey The constant that tells the database what value to update
	 * @param value The new value 
	 * @param context The context from the class that requests the update
	 */
	public static void saveSystemSetting(long profileId , String dbKey, Object value, Context context) {
		db = new SQLiteStorage(context);
        db.open();
        if (value.getClass().equals(String.class)) {
        	String stringValue = (String) value;
            db.updateSystemSettingValue(profileId, dbKey, stringValue);
        } else if (value.getClass().equals(Integer.class)) {
        	int intValue = (Integer) value;
            db.updateSystemSettingValue(profileId, dbKey, intValue);
        } else if (value.getClass().equals(Boolean.class)) {
        	boolean booleanValue = (Boolean) value;
            db.updateSystemSettingValue(profileId, dbKey, booleanValue);
        }
        db.close();
	}
	
	public static String getTextValue (long profileId , String dbKey, Context context) {
		db = new SQLiteStorage(context);
        db.open();
        String value = db.getTextValue(profileId, dbKey);
        db.close();
        return value;
	}
	
	public static int getIntValue (long profileId , String dbKey, Context context) {
		db = new SQLiteStorage(context);
        db.open();
        int value = db.getIntValue(profileId, dbKey);
        db.close();
        return value;
	}
	
	public static boolean getBooleanValue (long profileId , String dbKey, Context context) {
		db = new SQLiteStorage(context);
        db.open();
        boolean value = db.getBooleanValue(profileId, dbKey);
        db.close();
        return value;
	}
	
	public static void delete(long profileId, Context context) {
		db = new SQLiteStorage(context);
		db.open();
		db.deleteProfile(profileId);
		db.close();
	}
	
//	public static Cursor getCurrentSystemProfile(Context context) {
//		db = new SQLiteStorage(context);
//		db.open();
//		Cursor cursor = db.getCurrentSystemProfile();
//		db.close(); 
//		return cursor;
//	}

	public static long createSystemSettingsProfile(Context context) {
		db = new SQLiteStorage(context);
		db.open();
		long id = db.createSystemSettingsProfile();
		db.close();
		return id;
	}
}
